// Crypt.cpp: implementation of the CCrypt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Crypt.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCrypt::CCrypt()
{
}

CCrypt::~CCrypt()
{
}

//////////////////////////////////////////////////////////////////////////
//return pointer is in heap, so please delete it after has been used.
LPSTR CCrypt::StrToHex(LPSTR pstr, DWORD dwStrLen)
{
	char pRet[256]={0};
	if(!pstr || dwStrLen==0)
		return pRet;
	//pRet=new char[2*dwStrLen+1];
	//memset(pRet, 0, 2*dwStrLen+1);
	if(!pRet)
		return pRet;
	for(DWORD dwi=0; dwi<dwStrLen; dwi++){
		sprintf(&pRet[2*dwi], "%02x", (BYTE)pstr[dwi]);
	}
	memcpy(pstr, pRet, 256);
	//delete[] pRet;
	return pRet; 
}

//////////////////////////////////////////////////////////////////////////
//return pointer is in heap, so please delete it after has been used.
LPSTR CCrypt::HexToStr(LPSTR pstr, DWORD dwStrLen)
{
	if(!pstr || dwStrLen==0 || dwStrLen%2!=0)
		return NULL;
	char pRet[256]={0};
	if(!pRet)
		return pRet;
	char ch[3];
	ch[2]='\0';
	DWORD dwi;
	for(dwi=0; dwi<dwStrLen/2; dwi++)
	{
		ch[0]=pstr[dwi*2];
		ch[1]=pstr[dwi*2+1];
		sscanf(ch, "%02x", (LPBYTE)&pRet[dwi]);
	}
	pRet[dwi]='\0';
	memcpy(pstr, pRet, min(256, strlen(pRet)+1));
	return pRet;
}

LPSTR CCrypt::EnCode1(LPSTR strSrc, LPCSTR strKey) 
{
	LPSTR pRet=NULL;
	if(!strSrc || !strKey)
		return pRet;
	DWORD dwSrcLen=strlen(strSrc);
	DWORD dwKeyLen=strlen(strKey);
	if(dwSrcLen==0 || dwKeyLen==0)
		return pRet;
	//LPSTR pstr=new char[dwSrcLen+1];
	char pstr[256]={0};
	if(!pstr)
		return pRet;
	strcpy(pstr, strSrc);
	for(DWORD j=0; j<dwSrcLen; j++){ 
		pstr[j]^=~(strKey[j%dwKeyLen]>>(j%4));
	}
	StrToHex(pstr, dwSrcLen);
	//delete[] pstr;
	memcpy(strSrc, pstr, 256);
	//if (pRet)
	//	delete[] pRet;
	return pRet; 
} 

LPSTR CCrypt::DeCode1(LPSTR strSrc, LPCSTR strKey)
{
	LPSTR pRet=NULL;
	if(!strSrc || !strKey)
		return pRet;
	DWORD dwSrcLen=strlen(strSrc);
	DWORD dwKeyLen=strlen(strKey);
	if(dwSrcLen==0 || dwSrcLen%2!=0 || dwKeyLen==0)
		return pRet;
	char pstr[256] = {0};
	memcpy(pstr, strSrc, min(256, strlen(strSrc)));	

	HexToStr(pstr, dwSrcLen);

	for(DWORD j=0; j<dwSrcLen/2; j++){ 
		pstr[j]^=~(strKey[j%dwKeyLen]>>(j%4));
	}
	memcpy(strSrc, pstr, min(256, strlen(pstr)+1));
	return pRet;
}

LPSTR CCrypt::EnCode2(LPSTR strSrc, LPSTR strKey) 
{
	LPSTR pRet=NULL;
	//if(!strSrc || !strKey)
	//	return pRet;
	//DWORD dwSrcLen=strlen(strSrc);
	//DWORD dwKeyLen=strlen(strKey);
	//if(dwSrcLen==0 || dwKeyLen==0)
	//	return pRet;
	//LPSTR pstr=new char[dwSrcLen+1];
	//if(!pstr)
	//	return pRet;
	//strcpy(pstr, strSrc);
	//for(DWORD j=0; j<dwSrcLen; j++){ 
	//	pstr[j]^=~strKey[j%dwKeyLen];
	//}
	//pRet=StrToHex(pstr, dwSrcLen);
	//delete[] pstr;
	return pRet; 
} 

LPSTR CCrypt::DeCode2(LPSTR strSrc, LPSTR strKey)
{
	LPSTR pRet=NULL;
	if(!strSrc || !strKey)
		return pRet;
	DWORD dwSrcLen=strlen(strSrc);
	DWORD dwKeyLen=strlen(strKey);
	if(dwSrcLen==0 || dwSrcLen%2!=0 || dwKeyLen==0)
		return pRet;
	pRet=HexToStr(strSrc, dwSrcLen);
	if(!pRet)
		return pRet;
	for(DWORD j=0; j<dwSrcLen/2; j++){ 
		pRet[j]^=~strKey[j%dwKeyLen];
	}
	return pRet;
}

LPSTR CCrypt::EnCode3(LPSTR strSrc, LPSTR strKey) 
{
	LPSTR pRet=NULL;
	//if(!strSrc || !strKey)
	//	return pRet;
	//DWORD dwSrcLen=strlen(strSrc);
	//DWORD dwKeyLen=strlen(strKey);
	//if(dwSrcLen==0 || dwKeyLen==0)
	//	return pRet;
	//LPSTR pstr=new char[dwSrcLen+1];
	//if(!pstr)
	//	return pRet;
	//strcpy(pstr, strSrc);
	//for(DWORD j=0; j<dwSrcLen; j++){ 
	//	pstr[j]^=~(strKey[j%dwKeyLen]<<(j%2));
	//}
	//pRet=StrToHex(pstr, dwSrcLen);
	//delete[] pstr;
	return pRet; 
} 

LPSTR CCrypt::DeCode3(LPSTR strSrc, LPSTR strKey)
{
	LPSTR pRet=NULL;
	if(!strSrc || !strKey)
		return pRet;
	DWORD dwSrcLen=strlen(strSrc);
	DWORD dwKeyLen=strlen(strKey);
	if(dwSrcLen%2!=0 || dwKeyLen==0)
		return pRet;
	pRet=HexToStr(strSrc, dwSrcLen);
	if(!pRet)
		return pRet;
	for(DWORD j=0; j<dwSrcLen/2; j++){ 
		pRet[j]^=~(strKey[j%dwKeyLen]<<(j%2));
	}
	return pRet;
}
